package top.dprkshow.practice.queueArray;

/**
 * @author Kent HAN
 * @date 2021/8/11
 */
public class QueueArray {
    final int LENGTH = 6;
    final int CAPACITY = LENGTH - 1;
    int[] data = new int[LENGTH];
    int pop = 0, insert = 0;

    boolean isFull() {
        return pop == (insert + 1) % LENGTH;
    }

    void enqueue(int value) {
        if (isFull()) {
            throw new RuntimeException("Queue is full.");
        }
        data[insert] = value;
        insert = (insert + 1) % LENGTH;
    }

    void printDebug() {
        for (int i = pop; i < insert; i = (i + 1) % LENGTH) {
            System.out.print(data[i] + " -> ");
        }
        System.out.println();
    }

    boolean isEmpty() {
        return pop == insert;
    }

    int dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("Queue is empty");
        }
        int ret = data[pop];
        pop = (pop + 1) % LENGTH;
        return ret;
    }
}
